package com.sky.moduels.mini.service;

import com.sky.moduels.mini.event.SubscribeEvent;
import com.sky.moduels.mini.event.UnSubscribeEvent;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.builder.outxml.ImageBuilder;
import me.chanjar.weixin.mp.builder.outxml.TextBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Description: 微信消息业务处理
 * @ClassName: WxMessageService
 * @Author: hupengming
 * @CreateTime: 2022-08-19 15:12
 */
@Slf4j
@Service
public class WxMessageService {

    @Autowired
    ApplicationContext applicationContext;

    /**
     * 微信推送过来的消息
     *
     * @param request
     * @throws IOException
     */
    public String pullWxMessage(HttpServletRequest request) throws IOException {
        WxMpXmlMessage wxMpXmlMessage = WxMpXmlMessage.fromXml(request.getInputStream());
        String msgType = wxMpXmlMessage.getMsgType();
        log.info("【新消息】【消息类型】-{}", wxMpXmlMessage.getMsgType());

        log.info("----------------------华丽的分割线-----------------------------");

        log.info("【消息】{}",wxMpXmlMessage);

        switch (msgType) {
            case WxConsts.XmlMsgType.TEXT: // 文本消息
                return this.textMessage(wxMpXmlMessage);
            case WxConsts.XmlMsgType.IMAGE: // 图片消息
                return this.imageMessage(wxMpXmlMessage);
            case WxConsts.XmlMsgType.VOICE: // 语音消息
                break;
            case WxConsts.XmlMsgType.VIDEO: // 视频消息
                break;
            case WxConsts.XmlMsgType.LOCATION: // 地理位置消息
                break;
            case WxConsts.XmlMsgType.LINK: // 链接消息
                break;
            case WxConsts.XmlMsgType.EVENT: // 事件消息
                if(StringUtils.equals(WxConsts.EventType.SUBSCRIBE,wxMpXmlMessage.getEvent())){
                    log.info("【关注事件】");
                    applicationContext.publishEvent(new SubscribeEvent(wxMpXmlMessage.getFromUser()));
                    return this.subscribeMessage(wxMpXmlMessage);
                }

                if(StringUtils.equals(WxConsts.EventType.UNSUBSCRIBE,wxMpXmlMessage.getEvent())){
                    log.info("【取消关注事件】");
                    applicationContext.publishEvent(new UnSubscribeEvent(wxMpXmlMessage.getFromUser()));
                }

                return null;
            default:
                System.out.println(1);
        }
        return null;
    }

    /**
     * 图片消息
     *
     * @param wxMpXmlMessage
     * @return
     */
    public String imageMessage(WxMpXmlMessage wxMpXmlMessage){
        ImageBuilder image = WxMpXmlOutMessage.IMAGE();
        return image.mediaId("CVs913eYKg9eeFO93m-RV0Ekb3U_UfdyY3lNfheayK9mlo46S_fKA6qRRu5eYkiA")
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser()).build().toXml();

    }

    /**
     * 文本消息
     *
     * @param wxMpXmlMessage
     * @return
     */
    public String textMessage(WxMpXmlMessage wxMpXmlMessage) {
        log.info("【有文本新消息】");
        String outContent = "您好！";
        // 方式一
        TextBuilder textBuilder = new TextBuilder();
        return textBuilder.content(outContent)
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .build()
                .toXml();

        //方式二
/*        TextBuilder text = WxMpXmlOutTextMessage.TEXT();
        return text.content(outContent)
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .build()
                .toXml();*/

        // 方式三
/*        WxMpXmlOutTextMessage outMessage = new WxMpXmlOutTextMessage();
        outMessage.setFromUserName(wxMpXmlMessage.getToUser());
        outMessage.setToUserName(wxMpXmlMessage.getFromUser());
        outMessage.setCreateTime(Calendar.getInstance().getTimeInMillis());
        outMessage.setMsgType(WxConsts.MassMsgType.TEXT);
        outMessage.setContent(outContent);
        return outMessage.toXml();*/
    }


    /**
     * 关注消息
     *
     * @return
     */
    public String subscribeMessage(WxMpXmlMessage wxMpXmlMessage){
        StringBuffer buffer = new StringBuffer();
        buffer.append("感谢关注公众号！\n");
        buffer.append("该公众号服务诗词，音乐，名人名言，感兴趣就多看看哦...");
        // 方式一
        TextBuilder textBuilder = new TextBuilder();
        return textBuilder.content(buffer.toString())
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .build()
                .toXml();
    }

}
